local love = require 'love'
local fontHandler = require('assets-handler.font-handler')

local drawg = love.graphics.draw
local setColor = love.graphics.setColor
local printg = love.graphics.print
local drawRectangle = love.graphics.rectangle

local width = Width
local height = Height
local padding = Padding

local title = {
    text = "胜利",
    x = 0,
    y = 0,
    width = 0,
    height = 0,
    fontSize = 36,
    color = {0, 0, 0, 0.8}
}

local content = {
    x = 0,
    y = 0,
    fontSize = 18,
    textColor = {1, 1, 1},
    bgColor = {0, 0, 0, 0.7},
    margin = 10, -- 内边距
    text = {},
    lineHeight = 0,
    textHeight = 0,
    bgWidth = 0,
    bgHeight = 0,
    totalDamage = 0,
    highestDamage = 0,
    highestDamageType = "",
    mostCommonType = "",
    types = {
        {
            typeName = "royalflush",
            name = "皇家同花顺",
            times = 0,
            highest = 0
        },
        {
            typeName = "straightflush",
            name = "同花顺",
            times = 0,
            highest = 0
        },
        {
            typeName = "fullhouse",
            name = "三带二",
            times = 0,
            highest = 0
        },
        {
            typeName = "flush",
            name = "同花",
            times = 0,
            highest = 0
        },
        {
            typeName = "straight",
            name = "顺子",
            times = 0,
            highest = 0
        },
        {
            typeName = "fourofakind",
            name = "四条",
            times = 0,
            highest = 0
        },
        {
            typeName = "threeofakind",
            name = "三条",
            times = 0,
            highest = 0
        },
        {
            typeName = "twopairs",
            name = "两对",
            times = 0,
            highest = 0
        },
        {
            typeName = "pair",
            name = "对子",
            times = 0,
            highest = 0
        },
        {
            typeName = "singlehighcard",
            name = "单高牌",
            times = 0,
            highest = 0
        }
    }
}

local turn = 0
local match = 0
local vImage, vScale
local fImage, fScale
local maxHeight, upperLimit

local function init()
    fontHandler.setChineseFont(title.fontSize)
    title.height = fontHandler.getFontHeight()
    title.width = fontHandler.getFontWidth(title.text)
    title.x = (width - title.width) / 2
    title.y = height / 18

    fontHandler.setFont(content.fontSize)
    content.lineHeight = fontHandler.getFontHeight()
    content.x = padding
    content.bgWidth = width - padding * 2

    maxHeight = height - title.height - title.y - padding * 2
    upperLimit = title.y + title.height + padding

    vImage = love.graphics.newImage("assets/v.jpg")
    vScale = width / vImage:getWidth()

    fImage = love.graphics.newImage("assets/f.jpg")
    fScale = width / fImage:getWidth()
end

local function drawSettlement(settlement)
    if settlement == 1 then
        title.text = "胜利"
        drawg(vImage, 0, 0, 0, vScale, vScale)
    elseif settlement == 2 then
        title.text = "失败"
        drawg(fImage, 0, 0, 0, fScale, fScale)
    end

    fontHandler.setChineseFont(title.fontSize)
    setColor(title.color)
    printg(title.text, title.x, title.y)

    setColor(content.bgColor)
    drawRectangle("fill", content.x, content.y, content.bgWidth, content.bgHeight, 5)

    fontHandler.setFont(content.fontSize)
    setColor(content.textColor)
    for i, line in ipairs(content.text) do
        printg(line, content.x + content.margin, content.y + content.margin + ( i - 1 ) * content.lineHeight)
    end
end

local function settle(typeName, score, matchFinished)
    turn = turn + 1
    -- 查找对应类型的索引
    local typeIndex = nil
    for i, v in ipairs(content.types) do
        if v.typeName == typeName then
            typeIndex = i
            break
        end
    end

    if typeIndex then
        local t = content.types[typeIndex]
        t.times = t.times + 1
        content.totalDamage = content.totalDamage + score

        -- 更新该类型的最高分
        if score > t.highest then
            t.highest = score
        end

        -- 更新全局最高分
        if score > content.highestDamage then
            content.highestDamage = score
            content.highestDamageType = t.name
        end
    end

    -- 对牌型按次数从大到小排序
    table.sort(content.types, function(a, b)
        return a.times > b.times
    end)

    -- 更新最常见类型索引
    if #content.types > 0 then
        content.mostCommonType = content.types[1].name  -- 排序后第一个元素是最常见的
    end

    -- 更新详细数据统计
    if matchFinished then
        match = match + 1
        content.text = {}
        -- 更新详细数据统计
        content.text = {
            "单次最高伤害：" .. content.highestDamage,
            "单次最高伤害牌型：" .. content.highestDamageType,
            "总计伤害：" .. content.totalDamage,
            "总回合数: " .. turn,
            "打败了怪物数量: " .. match,
            "最常用牌型：" .. content.mostCommonType
        }
        -- 添加牌型统计（次数大于0的）
        for _, t in ipairs(content.types) do
            if t.times > 0 then
                table.insert(content.text, t.name .. "出现次数: " .. t.times)
            end
        end

        -- 计算文本区域的宽度和高度
        content.textHeight = #content.text * content.lineHeight
        content.bgHeight = content.textHeight + padding * 2

        if content.bgHeight > maxHeight then
            content.y = upperLimit
        else
            content.y = height - content.bgHeight - padding
        end
    end
end

local function reset()
    content.totalDamage = 0
    content.highestDamage = 0
    content.highestDamageType = ""
    content.mostCommonType = ""
    for _, t in ipairs(content.types) do
        t.times = 0
        t.highest = 0
    end
    content.text = {}
    turn = 0
    match = 0
end

return{
    init = init,
    drawSettlement = drawSettlement,
    settle = settle,
    reset = reset,
}